<template>
  <div class="login-container">
    <div ref="loginWrapper" class="login-bg-wrapper"></div>
    <div class="flex form-wrapper">
      <div class="left"></div>
      <div class="right">
        <div class="my-width flex-sub flex justify-center align-center">
          <div class="title margin-left">智慧档案RFID系统</div>
        </div>
        <div class="form-container">
          <div class="item-wrapper">
            <el-input v-model="username" placeholder="请输入用户名" prefix-icon="el-icon-user" clearable />
          </div>
          <div class="item-wrapper margin-top-lg">
            <el-input v-model="password" placeholder="请输入密码" type="password" clearable show-password
              prefix-icon="el-icon-lock" />
          </div>
          <div class="item-wrapper">
            <VawVerify :key="key" @verify-success="onVerifySuccess" />
          </div>
          <div class="flex-sub"></div>
          <div class="margin-top-lg">
            <el-button type="primary" class="login" @click="login"> 登录 </el-button>
          </div>
        </div>
      </div>
    </div>
    <loginAnimation></loginAnimation>
  </div>
</template>

<script>
import { sysLogin } from '@/api/request'
import loginAnimation from './loginAnimation.vue'
import ImageBg1 from '@/assets/login_left.jpg'
import VawVerify from 'vaw-verify'
import 'vaw-verify/lib/vaw-verify.css'
export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: 'Login',
  components: { VawVerify, loginAnimation },
  data() {
    return {
      username: 'admin',
      password: '123456',
      ImageBg1,
      redirect: '',
      autoLogin: true,
      verifyState: false,
      key: 0,
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
        }
      },
      immediate: true,
    },
  },
  mounted() {
    window.addEventListener(
      'resize',
      this.throttle(() => {
        // 在这里重新渲染组件的逻辑
        this.key++
        // 重新渲染组件的代码
      }, 1000)
    )
  },
  methods: {
    throttle(func, wait) {
      let timer
      return function () {
        if (!timer) {
          timer = setTimeout(() => {
            func.apply(this, arguments)
            timer = null
          }, wait)
        }
      }
    },
    // 登录
    login() {
      if (!this.username) {
        this.$message.error('请输入用户名')
        return
      }
      if (!this.password) {
        this.$message.error('请输入密码')
        return
      }
      if (!this.verifyState) {
        this.$message.error('滑动验证失败')
        return
      }
      let params = {
        loginName: this.username,
        password: this.password,
      }
      sysLogin(params).then((res) => {
        if (res.data.code == 200) {
          this.$message({
            message: res.data.message,
            type: 'success',
          })
          localStorage.setItem('X-Access-Token', res.data.data.token)
          localStorage.setItem('userName', res.data.data.userInfo.realName)
          localStorage.setItem('createBy', res.data.data.userInfo.createBy)
          localStorage.setItem('loginName', res.data.data.userInfo.loginName)
          localStorage.setItem('accountId', res.data.data.userInfo.accountId)
          this.$store.commit('setUserId', res.data.data.userInfo.accountId)
          document.documentElement.style.setProperty(
            '--menuItemHoverBackgroundColor',
            res.data.data.userInfo.theme
          )
          localStorage.setItem('theme', res.data.data.userInfo.theme)
          this.$store.commit('setTheme', res.data.data.userInfo.theme)
          this.$router.push({ path: '/sysIndex' })
        } else {
          this.$message.error(res.data.message)
        }
      })
      // this.$router.push({ path: '/sysIndex' })
    },
    onVerifySuccess() {
      this.verifyState = true
    },
  },
}
</script>

<style lang="less" scoped>
.login-container {
  position: relative;
  overflow: hidden;
  height: 100vh;
  width: 100%;

  .login-bg-wrapper {
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 100%;
    background-image: linear-gradient(to top, #09203f 0%, #537895 100%);

    &>img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
  }

  .login-footer-wrapper {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;

      {
      .el-card {
        background-color: transparent;
      }
    }
  }

  .form-wrapper {
    background: url('../assets/login_left.jpg') no-repeat;
    background-size: cover;
    overflow: hidden;
    z-index: 999;
    width: 50%;
    height: 55%;
    position: absolute;
    top: 50%;
    // 水平垂直居中
    transform: translate(-50%, -50%);
    left: 50%;
    right: 50%;
    bottom: 15.8%;
    border-radius: 10px;

    .form-container {
      flex-grow: 1;

      .item-wrapper {
        margin: 0 0 20px 0;
      }
    }

    @media screen and (max-width: 768px) {
      .left {
        display: none;
      }

      .right {
        width: 100%;
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        align-items: center;

        .my-width {
          width: 80%;
          flex-grow: 1;
        }

        .title {
          display: block;
          text-align: center;
          font-size: 20px;
          font-weight: bold;
          color: #34495e;
          text-shadow: 0 0 0.2em #41b883, -0 -0 0.2em #41b883;
        }

        .form-container {
          flex-grow: 1;
          width: 80%;
          min-height: 60%;
          text-align: center;
          background: rgba(183, 183, 183, 0.2);
          padding: 5%;
          border-radius: 5px;
          border: 2px solid #fff;
          display: flex;
          flex-direction: column;
          justify-content: space-between;

          .item-wrapper {
            width: 100%;
          }

          .login {
            width: 100%;
          }
        }
      }
    }

    @media screen and (min-width: 768px) and (max-width: 992px) {
      .left {
        display: none;
      }

      .right {
        display: flex;
        width: 100%;
        flex-direction: column;
        justify-content: space-around;
        align-items: center;

        .my-width {
          width: 48%;
          flex-grow: 1;
        }

        .title {
          display: block;
          text-align: center;
          font-size: 20px;
          font-weight: bold;
          color: #5497ff;
        }

        .form-container {
          flex-grow: 1;
          width: 50%;
          height: 60%;
          margin-bottom: 10%;
          text-align: center;
          background: rgba(183, 183, 183, 0.2);
          padding: 5%;
          border-radius: 5px;
          border: 2px solid #fff;
          display: flex;
          flex-direction: column;
          justify-content: space-between;

          .item-wrapper {
            width: 100%;
          }

          .login {
            width: 100%;
          }
        }
      }
    }

    @media screen and (min-width: 992px) {
      .left {
        display: block;
        flex: 1;
      }

      .right {
        margin-left: 15%;
        flex: 1;
        display: flex;
        justify-content: center;
        flex-direction: column;

        .my-width {
          width: 80%;
          flex-grow: 1;
        }

        .title {
          display: block;
          font-size: 24px;
          font-weight: bold;
          color: #5497ff;
        }

        .item-wrapper {
          width: 80%;
        }

        .login {
          width: 80%;
        }
      }
    }
  }
}
</style>
